package com.controller.library;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.*;

import com.action.library.TLoginAction;
import com.bean.library.Teacher;

public class TLoginServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String teacherId = request.getParameter("user_name");
		String password = request.getParameter("user_password");
		
		/*
		 * 
		 * 这里需要session教师的Id 
		 * request.getSession().setAttribute("teacherId",123456);
		 * 
		 */
		TLoginAction tLoginAction = new TLoginAction();
		Teacher teacher = tLoginAction.teacherLogin(teacherId, password);
		if(teacher!=null){
			request.getSession().setAttribute("teacher", teacher);
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_welcome.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_login_incorrect.htm");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
//		if(userName.equals("tomato_team") && userPass.equals("123")){
//			try {
//				response.sendRedirect("/Exam1/teacher/t_page/t_welcome.htm");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		else{
//			try {
//				response.sendRedirect("/Exam1/teacher/t_page/t_login_incorrect.htm");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
	}
}
